/* -*- C++ -*- */

//=============================================================================
/*
 * @author Feloweria <380374483@qq.com>
 * Copyright (C) Feloweria
 * 
 * $ID 2013-07-13 04:41:13 PM
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @version 1.0
 */
//=============================================================================

#include "Log/Log.h"
#include "Lualib/LuaVM.h"
#include "LuaKernel/lua.hpp"

FELOWRIA_BEGIN

void LuaError(lua_State* L)
{
	lua_gettop(L);
	const char* msg = lua_tostring(L, -1);
	LOG_ERROR(msg);
	lua_pop(L, 1);
}


LuaVM::LuaVM()
{
	L = luaL_newstate();
	luaL_openlibs(L);
}

LuaVM::~LuaVM()
{
	lua_close(L);
}

bool LuaVM::LoadFile(const UTF8 *file_name)
{
	if (luaL_loadfile(L, file_name))
	{
		LuaError(L);
		return false;
	}

	if (lua_pcall(L, 0, 0, 0))
	{
		LuaError(L);
		return false;
	}

	return true;
}

FELOWRIA_END
